package net.anzhuo.controller.db;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsBase;
import net.anzhuo.entity.GsCompanyProducts;
import net.anzhuo.entity.db.CompanyProducts;
import net.anzhuo.entity.db.CompanyProductsResult;
import net.anzhuo.entity.db.shangbiao.Paging;
import net.anzhuo.service.GsBaseService;
import net.anzhuo.service.GsCompanyProductsService;

@RestController
@Slf4j
public class CompanyProductsController {

	@Autowired
	private GsBaseService gsBaseService;


	
	


	
	
	@Autowired
	private GsCompanyProductsService gsCompanyProductsService;
	
	

	
	
	
	@RequestMapping("/CompanyProductV4/SearchCompanyCompanyProducts")
	public CompanyProductsResult SearchCompanyCompanyProducts(String searchKey,String pageSize,String pageIndex,String dtype) {
		log.info("===>SearchCompanyCompanyProducts="+searchKey);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		CompanyProductsResult rr = null;
		try {
			rr = (CompanyProductsResult)RedisUtil.get("SearchCompanyCompanyProducts:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" SearchCompanyCompanyProducts 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		CompanyProductsResult detailResult = new CompanyProductsResult();
		try {
	
		detailResult.setStatus("200");
		detailResult.setMessage("查询成功");
		 GsCompanyProducts gsQuery = new GsCompanyProducts();
		GsBase base = gsBaseService.getByName(searchKey);
		if(base!=null)
		{
		gsQuery.setCoMd5(base.getMd5());
		}else
		{
			gsQuery.setCoMd5(searchKey);
		}
		gsQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsCompanyProducts>  gsCompanyProductsList=gsCompanyProductsService.getListWithPage(gsQuery);
        Paging paging=new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(gsCompanyProductsList.getTotalCount());
		detailResult.setPaging(paging);
		
		List<CompanyProducts> companyProducts=new ArrayList<CompanyProducts>();
		if(gsCompanyProductsList!=null&&gsCompanyProductsList.getList().size()>0)
		{
		   CompanyProducts cp=new CompanyProducts();
		   List<GsCompanyProducts> list=gsCompanyProductsList.getList();
		   for(GsCompanyProducts c:list)
		   {
			   cp=new CompanyProducts();
			   cp.setCompanyName(c.getCompanyName());
			   cp.setLink(c.getLink());
			   cp.setImageUrl(c.getImageUrl());
			   cp.setName(c.getName());
			   cp.setDomain(c.getDomain());
			   cp.setTags(c.getTags());
			   cp.setDescription(c.getDescription());
			   cp.setCategory(c.getCategory());
			   companyProducts.add(cp);
		   }
		}
		
	    
		detailResult.setResult(companyProducts);
		}catch(Exception e1)
		{
			log.info(e1.getMessage());
		}
		
		try {
			RedisUtil.set("SearchCompanyCompanyProducts:"+searchKey, detailResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" SearchCompanyCompanyProducts 执行时间："+excTime+"s");
		return detailResult;
	}
}
